package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.TransactionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.TransactionForm;
import com.youlai.boot.system.model.query.TransactionQuery;
import com.youlai.boot.system.model.vo.TransactionVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 交易记录表前端控制层
 *
 * @author youlaitech
 * @since 2025-02-27 15:58
 */
@Tag(name = "交易记录表接口")
@RestController
@RequestMapping("/api/v1/transactions")
@RequiredArgsConstructor
public class TransactionController  {

    private final TransactionService transactionService;

    @Operation(summary = "交易记录表分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('sys:transaction:query')")
    public PageResult<TransactionVO> getTransactionPage(TransactionQuery queryParams ) {
        IPage<TransactionVO> result = transactionService.getTransactionPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增交易记录表")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:transaction:add')")
    public Result<Void> saveTransaction(@RequestBody @Valid TransactionForm formData ) {
        boolean result = transactionService.saveTransaction(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取交易记录表表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('sys:transaction:edit')")
    public Result<TransactionForm> getTransactionForm(
        @Parameter(description = "交易记录表ID") @PathVariable Long id
    ) {
        TransactionForm formData = transactionService.getTransactionFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改交易记录表")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('sys:transaction:edit')")
    public Result<Void> updateTransaction(
            @Parameter(description = "交易记录表ID") @PathVariable Long id,
            @RequestBody @Validated TransactionForm formData
    ) {
        boolean result = transactionService.updateTransaction(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除交易记录表")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:transaction:delete')")
    public Result<Void> deleteTransactions(
        @Parameter(description = "交易记录表ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = transactionService.deleteTransactions(ids);
        return Result.judge(result);
    }
}
